import { defineStore } from 'pinia'
import { getHomeMutidata } from '@/service/home.js'
import { getHomeData } from '../service/home'

export const types = ['pop', 'new', 'sell']
function MapGoodListType() {
  let goodListOrign = {}
  types.forEach((type) => {
    goodListOrign[type] = { page: 0, list: [] }
  })
  return goodListOrign
}

export const useHomeStore = defineStore('home', {
  state: () => {
    return {
      banners: [],
      recommends: [],
      goodsList: MapGoodListType(),
      currenType: 'pop'
    }
  },
  actions: {
    setCurrentType(type) {
      this.currenType = type
    },
    async fetchHomeMultidata() {
      const res = await getHomeMutidata()
      // console.log('fetchHomeMultidata',res)
      this.banners = res.banner.list
      this.recommends = res.recommend.list
    },
    async fetchHomeData(type, page) {
      const res = await getHomeData(type, page)
      // console.log(res.list)
      this.goodsList[type].list.push(...res.list)
      this.goodsList[type].page = page
      console.log('更新拉')
    }
  }
})
